Skip to content

Add instrumentation plugin API to Amazon.Lambda.DurableExecution (groundwork for OpenTelemetry) - #2611

Draft
normj wants to merge 9 commits into
masterfrom
normj/durable-otel-plugin-api
Draft

normj wants to merge 9 commits into
masterfrom
normj/durable-otel-plugin-api

Conversation

@normj

@normj normj commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Issue #, if available:

Description of changes:

Draft. Opened early so others can follow the work. The core plugin API in this PR is complete; the pieces after it are separate PRs.

Background

The Python, JavaScript and Java durable execution SDKs added OpenTelemetry support in Core 3.0 / OTel 2.0. In all three, the core SDK has a generic instrumentation plugin framework with no knowledge of tracing, and OTel is one plugin built on it. This PR adds the same framework to Amazon.Lambda.DurableExecution.

What this PR adds

  • Plugin API in Amazon.Lambda.DurableExecution.Instrumentation: IDurableInstrumentationPlugin (every hook optional), IDurableInstrumentationPluginFactory (a new plugin per invocation, optional ExclusiveGroup), info types, PluginLoadException, and DurableExecutionOptions.Plugins.
  • New DurableFunction.WrapAsync overloads that take DurableExecutionOptions. Existing signatures are unchanged, and with no plugin registered the code path is unchanged.
  • Hooks: invocation start and end (Succeeded, Failed, Pending, or Retrying when the invocation throws), operation start and end for every operation type, attempt start and end for steps and wait-for-condition polls, OnOperationChange for operations a checkpoint response adds or changes, and WrapAsync around the invocation, each attempt and each child context. Map, parallel and incremental parallel and their branches are covered.
  • Chained-invoke trace propagation: plugins can supply a header, which is written to ChainedInvokeOptions.XAmznTraceId on the invoke START. This raises the AWSSDK.Lambda minimum from 4.0.13.1 to 4.0.110.1, the first version with that field.
  • Log scope: the durable logger adds the fields a plugin returns from GetLogScope to the scope of every entry.
  • Testing: TestRunnerOptions.ExecutionOptions runs plugins in the local test runner.
  • Docs: user guide and a design note with the decisions behind the hook contract.

Contract

  • A plugin cannot fail the workflow: hook exceptions are logged and ignored, and WrapAsync always runs the user code exactly once and lets its exception through.
  • The operation and attempt hooks are synchronous and never awaited, so a plugin cannot delay an operation or the decision to suspend. Async work belongs in OnInvocationEndAsync, which is bounded by the remaining invocation time.
  • Two factories in one exclusive group fail at startup.

Checked against a real durable execution (us-west-2)

I ran a plugin that logs every hook in a deployed function (a step that fails once and retries, a wait, a child context, a parallel, a chained invoke, a terminal step failure, and a callback completed externally). That test function is not part of this PR. It confirmed:

  • ILambdaContext.TraceId is the same on every invocation of an execution and equals _X_AMZN_TRACE_ID.
  • UpdatedOperationIds is sent by the service and parses correctly.
  • The service accepts XAmznTraceId on a chained invoke and delivers it to the invoked function.
  • Plugin log scope fields appear in the function's JSON logs.
  • StepDetails.Attempt counts finished attempts, so a finished record already holds its last attempt number. I had it one too high on finished records; fixed in this PR.
  • A callback is registered long before it finishes. Its operation now ends when the result is read, not when CreateCallbackAsync returns. Also fixed.
  • A step resumed after a retry was reported as a fresh start (IsReplay false) by the invocation that ran its second attempt. IsReplay at start is now true whenever the operation already has a checkpoint; at end it still means the operation had already finished. Found while tracing a real execution with the OpenTelemetry plugin ([Draft, stacked on #2611] Add the OpenTelemetry tracing plugin (Execution and Invocation views) #2612).
  • StartTimestamp/EndTimestamp were dropped when mapping checkpoint responses, so operations that finished in the current invocation had no service timestamps. They are mapped now.

Not checked: Lambda Managed Instances, and the behavior of a very large number of operations.

Stack

This is the bottom of a stack of draft PRs:

  1. This PR: the plugin API.
  2. [Draft, stacked on #2611] Add the OpenTelemetry tracing plugin (Execution and Invocation views) #2612: Amazon.Lambda.DurableExecution.OpenTelemetry, the OTel plugin, using System.Diagnostics.ActivitySource (no OpenTelemetry NuGet dependency).
  3. Add Tracing and PluginProvider options to [DurableExecution] #2614: Annotations [DurableExecution(Tracing = ..., PluginProvider = ...)].
  4. Add OpenTelemetry conformance handlers for the durable execution SDK #2615: handlers for the shared OpenTelemetry conformance suites. Both views pass 25 of 27 cases against X-Ray; the other 2 need SDK features .NET does not have and are declared not implemented.

Not yet done: the shared plugin conformance suite (10-1 to 10-23) for this API.

Out of scope for now: the Durable Execution Telemetry exporter.

Testing

Amazon.Lambda.DurableExecution.Tests (629) and Amazon.Lambda.DurableExecution.Testing.Tests (156) pass on net8.0 and net10.0. New tests cover the runner, invocation wiring, operation and attempt hooks, callbacks, change notifications, log scope, propagation, attempt numbering and timestamp mapping. For the behaviors that matter most I checked that the tests fail when the behavior is removed.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@normj normj changed the title [WIP] Add instrumentation plugin API to Amazon.Lambda.DurableExecution (groundwork for OpenTelemetry) Add instrumentation plugin API to Amazon.Lambda.DurableExecution (groundwork for OpenTelemetry) Nov 17, 2026
@normj
normj added this pull request to stack #2616 November 18, 2026 23:03
Adds IDurableInstrumentationPlugin, its factory, the info types and
DurableExecutionOptions.Plugins, plus an internal PluginRunner that dispatches
hooks with per-plugin exception isolation, ordered start/end, a WrapAsync chain
that always runs the user code exactly once, first-wins trace propagation and a
bounded OnInvocationEndAsync. Not wired into DurableFunction yet.
Adds WrapAsync overloads that take DurableExecutionOptions. A plugin is created
per invocation, gets the invocation start and end hooks and wraps the workflow
function. The invocation ends as Succeeded, Failed, Pending, or Retrying when
the invocation throws. InvocationInfo carries the trace header from
ILambdaContext.TraceId and the operations the service reports as updated
(UpdatedOperationIds). With no plugins registered the code path is unchanged.
…gins

DurableOperation.ExecuteAsync reports OperationStart when the workflow reaches
an operation and OperationEnd when it reaches a terminal state (or is replayed
as terminal). Operations that suspend, or are cancelled, report no end. Steps
and wait-for-condition polls report AttemptStart/AttemptEnd and run their user
function inside the plugins' WrapAsync; child contexts wrap their function the
same way. Map, parallel, incremental parallel and their branches carry the
plugins too. OperationInfo.Attempt is the 1-based latest attempt (the service
records the number of earlier attempts). Operations without plugins take the
unchanged code path.
The durable logger adds the fields a plugin returns from GetLogScope to the
scope of every entry, read per entry because they follow the active span. A
chained invoke asks the plugins for a trace header after the payload is
serialized and writes it to ChainedInvokeOptions.XAmznTraceId on the START
checkpoint; an invoke that is replayed does not ask again. Raises the
AWSSDK.Lambda minimum to 4.0.110.1, the first version with that field.
…nner

Operations that a checkpoint response adds or whose status it changes (a
callback completed by an external system, a timer that fired) are reported
through OnOperationChange with a snapshot of every known operation. A response
that repeats a known status reports nothing.

TestRunnerOptions.ExecutionOptions passes DurableExecutionOptions to every
invocation of a local test run so plugins can be tested end to end.
Found by running a plugin against real durable executions in us-west-2.

- StepDetails.Attempt counts finished attempts. A finished step record
  already holds the number of its last attempt (SUCCEEDED on the second try
  reports 2), while a ready or pending one is on the attempt after it. The
  attempt number was one too high on finished records.
- A callback is registered long before it finishes. Its operation now ends
  when the result is read (replayed from history, failed or timed out), not
  when CreateCallbackAsync returns the handle.
- StartTimestamp and EndTimestamp were not copied from checkpoint responses
  and state pages, so operations that finished in the current invocation had
  no service timestamps. They are mapped now.
Adds a user guide for plugin authors (registration, hooks, guarantees, what
the service reports, testing) and a design note with the decisions behind the
hook contract. Links them from the README, the Testing README and CLAUDE.md,
which lists the rules to keep when changing the plugin code.
Found by tracing a real execution: a step that was retried reached OnOperationStart
as a fresh start in the invocation that ran its second attempt, so a plugin
that derives an id from the operation id used it twice.

IsReplay on OperationStart is now true whenever the operation already has a
checkpoint when the workflow reaches it, not only when it had finished. That
covers a step resumed after a retry and a child context that suspended. On
OperationEnd it still means the operation had already finished, so the resumed
step's end is not a replay.
@normj
normj force-pushed the normj/durable-otel-plugin-api branch from ff53240 to 4ddfe68 Compare November 18, 2026 23:41
Read from ILambdaContext.W3C, which carries clientContext.w3c. Lets a plugin
extract a W3C trace context the way it already can an X-Ray one.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant